﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;



namespace Pong
{
  
    public class level
    {
        private puck _puck;

        private puck _playerOnePuck;
        private puck _PlayerTwoPuck;

        private Vector2 _playerOneOldPosition;
        private Vector2 _playerTwoOldPosition;

        private Texture2D _board;
        private Texture2D _boardGlow;

        private Collision _collision = new Collision();
        private Rectangle _mapRectangle;

        private ParticleManager _particleManager;

        public level(ContentManager ContentManager)
        {
            _particleManager = new ParticleManager(ContentManager);

            _playerOnePuck = new puck("puck", ContentManager);
            _PlayerTwoPuck = new puck("puck", ContentManager); 

            _puck = new puck("puck", ContentManager);
            _board = ContentManager.Load<Texture2D>("board");
            _boardGlow = ContentManager.Load<Texture2D>("Glow_Background");
            _puck.Location = new Vector2(Globals.Width / 2, Globals.Height / 2);
            _mapRectangle = new Rectangle(0, 0, Globals.Width, Globals.Height);
            _puck.Velocity = new Vector2(1, 5);
        }
        public void Update(GameTimer gametime)
        {
            TouchCollection touchCollection = TouchPanel.GetState();
            foreach (TouchLocation tl in touchCollection)
            {

                if (tl.State == TouchLocationState.Moved)
                {
                    if (tl.Position.Y < (int)SharedGraphicsDeviceManager.DefaultBackBufferHeight - 40)
                    {
                        if (tl.Position.Y > (Globals.Height / 2) + 20)
                        {
                            _PlayerTwoPuck.Velocity = tl.Position - _playerTwoOldPosition;
                            _PlayerTwoPuck.Location = tl.Position;
                            _playerTwoOldPosition = tl.Position;
                        }
                        else if (tl.Position.Y < (Globals.Height / 2) - 20)
                        {
                            _playerOnePuck.Velocity = tl.Position - _playerOneOldPosition;
                            _playerOnePuck.Location = tl.Position;
                            _playerOneOldPosition = tl.Position;
                        }

                    }
                }

            }

            _puck.Update();
           // _puck.Update();
            Vector2 DirectionPuckCollision = _collision.InnerRectangleCollisionCheck(_puck.GetRectangle(), _mapRectangle);
            if (DirectionPuckCollision != new Vector2(0,0))
            {
               _puck.Location= _collision.InnerRectangleOffset(_puck.GetRectangle(), _mapRectangle, DirectionPuckCollision) + new Vector2(_puck.GetSize/2,_puck.GetSize/2);
               if (DirectionPuckCollision == new Vector2(0, 1)||DirectionPuckCollision == new Vector2(0,-1))
               {
  
                   _puck.Velocity = new Vector2(_puck.Velocity.X, _puck.Velocity.Y*-1);

               }
               else if (DirectionPuckCollision == new Vector2(1, 0) || DirectionPuckCollision == new Vector2(-1, 0))
               {


                   _particleManager.AddParticle(20, _puck.Location + new Vector2(_puck.GetSize / 2, 0), (3.14f / 2), 3.14f, 15, 30, Color.Red);
        
                   
                   _puck.Velocity = new Vector2(_puck.Velocity.X * -1, _puck.Velocity.Y);
               }

            }
           
            float PlayerOnePenetration = _collision.circlePenetration(_puck.Location, _puck.GetSize, _playerOnePuck.Location, _playerOnePuck.GetSize);
            float PlayerTwoPenetration = _collision.circlePenetration(_puck.Location, _puck.GetSize, _PlayerTwoPuck.Location, _PlayerTwoPuck.GetSize);
            _puck.Location += (Vector2.Normalize(_playerOnePuck.Location-_puck.Location) * new Vector2(PlayerOnePenetration, PlayerOnePenetration));
            _puck.Location += (Vector2.Normalize(_PlayerTwoPuck.Location-_puck.Location) * new Vector2(PlayerTwoPenetration, PlayerTwoPenetration));
            if (PlayerOnePenetration != 0.0f)
            {
                _puck.Velocity = (_puck.Velocity * -1) + _playerOnePuck.Velocity;
 
            }
            if (PlayerTwoPenetration != 0.0f)
            {
                _puck.Velocity = (_puck.Velocity * -1) + _PlayerTwoPuck.Velocity;
   
            }

            _particleManager.Update();
        }

        public void Draw(SpriteBatch SpriteBatch)
        {
            SpriteBatch.Draw(_board, new Rectangle(0, 0,Globals.Width, Globals.Height), Color.White);
            _puck.Draw(SpriteBatch);
            _playerOnePuck.Draw(SpriteBatch);
            _PlayerTwoPuck.Draw(SpriteBatch);
        }
        public void DrawGlow(SpriteBatch SpriteBatch)
        {
            _particleManager.Draw(SpriteBatch);
            SpriteBatch.Draw(_boardGlow, new Rectangle(0, 0, Globals.Width, Globals.Height), Color.White);
            _puck.DrawGlow(SpriteBatch);
        }
    }
}
